package com.imooc.service.impl;

import com.imooc.mapper.DbStuMapper;
import com.imooc.mapper.DbStuMapperCustom;
import com.imooc.pojo.DbStu;
import com.imooc.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

@Service
public class StuServiceImpl implements StuService {

    @Autowired
    private DbStuMapper stuMapper;

    @Autowired
    private DbStuMapperCustom stuMapperCustom;

    @Override
    public void saveStu(DbStu stu) {
        stuMapper.insert(stu);
    }

    @Override
    public DbStu queryById(String id){
        return stuMapper.selectByPrimaryKey(id);
    }

    @Override
    public DbStu queryByIdCustom(String id) throws InterruptedException {
        Thread.sleep(5000);
        List<DbStu> list = stuMapperCustom.getStuById(id);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    //多样化查询，模糊查询
//    @Override
//    public List<DbStu> queryByCondition(String name, Integer sex) {
//        Example example = new Example(DbStu.class);
//        Example.Criteria criteria = example.createCriteria();
//        //criteria.andEqualTo("name", name);
//        criteria.andEqualTo("sex", sex);
//        List<DbStu> list = stuMapper.selectByExample(example);
//        return list;
//    }

    @Override
    public List<DbStu> queryByCondition(String name, Integer sex) {
        //条件对象
        DbStu stu = new DbStu();
        stu.setName(name);
        stu.setSex(sex);

        List<DbStu> list = stuMapper.select(stu);
        return list;
    }

    @Override
    public void updateStu(DbStu stu) {
        stuMapper.updateByPrimaryKey(stu);
    }

    @Override
    public void deleteStu(DbStu stu) {
        //主键删除
        //stuMapper.deleteByPrimaryKey(stu.getId());
        //根据对象中属性值匹配条件删除
        //stuMapper.delete(stu);
        //根据构建的example进行条件删除
        Example example = new Example(DbStu.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("name", stu.getName());
        stuMapper.deleteByExample(example);
    }

    //事务类型 枚举
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void testTrans() {
        //1.新增数据
        //2.修改
        //3.模拟发生异常
        //4.观察1.2.步骤发生的数据变动，有没有影响到数据库
        //5.处理事物，实现事务回滚，不然先前的数据入库

        String sid = UUID.randomUUID().toString();
        DbStu stu = new DbStu();
        stu.setId(sid);
        stu.setName("66");
        stu.setSex(2);

        stuMapper.insert(stu);

        int a = 100 / 0;

        DbStu stuUpdate = new DbStu();
        stuUpdate.setId(sid);
        stu.setName("aaa");
        stu.setSex(2);
        stuMapper.updateByPrimaryKey(stuUpdate);
    }
}
